package com.sugar.psych.assessment.module.business.controller.admin.user;

import com.sugar.psych.assessment.module.infra.controller.admin.file.vo.file.FileUploadReqVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import java.io.IOException;
import java.util.*;

import com.sugar.psych.assessment.framework.common.pojo.PageResult;
import com.sugar.psych.assessment.framework.common.pojo.CommonResult;

import static com.sugar.psych.assessment.framework.common.pojo.CommonResult.success;

import com.sugar.psych.assessment.module.business.controller.admin.user.vo.*;
import com.sugar.psych.assessment.module.business.service.user.UserService;

@Tag(name = "管理后台 - 用户信息")
@RestController
@RequestMapping("/business/user")
@Validated
public class BizUserController {

    @Resource
    private UserService userService;

    @PostMapping("/create")
    @Operation(summary = "创建用户信息")
    @PreAuthorize("@ss.hasPermission('business:user:create')")
    public CommonResult<Long> createUser(@Valid @RequestBody UserSaveReqVO createReqVO) {
        return success(userService.createUser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户信息")
    @PreAuthorize("@ss.hasPermission('business:user:update')")
    public CommonResult<Boolean> updateUser(@Valid @RequestBody UserSaveReqVO updateReqVO) {
        userService.updateUser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:user:delete')")
    public CommonResult<Boolean> deleteUser(@RequestParam("id") Long id) {
        userService.deleteUser(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除用户信息")
                @PreAuthorize("@ss.hasPermission('business:user:delete')")
    public CommonResult<Boolean> deleteUserList(@RequestParam("ids") List<Long> ids) {
        userService.deleteUserListByIds(ids);
        return success(true);
    }

    @PostMapping("/import-excel")
    @Operation(summary = "导入用户认证信息")
    @PreAuthorize("@ss.hasPermission('business:user:create')")
    public CommonResult<Boolean> createUser(FileUploadReqVO uploadReqVO) throws IOException {
        userService.importExcel(uploadReqVO.getFile());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:user:query')")
    public CommonResult<UserRespVO> getUser(@RequestParam("id") Long id) {
        return success(userService.getUser(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户信息分页")
    @PreAuthorize("@ss.hasPermission('business:user:query')")
    public CommonResult<PageResult<UserRespVO>> getUserPage(@Valid UserPageReqVO pageReqVO) {
        return success(userService.getUserPage(pageReqVO));
    }
}